using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace UglyAnimals
{
    class Animation
    {
        // Image representing the sprite sheet
        Texture2D spriteStrip;

        // The scale used to display the sprite strip
        float scale;

        // Time Time since the last update
        int elapsedTime;

        // The time a frame is displayed until it changes
        int frameTime;

        // The number of frames the spritesheet contains
        int frameCount;

        // The index of the current frame
        int currentFrame;

        // The color of the frame to be displayed
        Color color;

        // The area of the image strip to be displayed
        Rectangle sourceRect;

        // The area where where the image strip will be displayed in the game
        Rectangle destinationRect;


        // The width of the frame
        public int FrameWidth
        {
            get;
            set;
        }

        // The height of the frame
        public int FrameHeight
        {
            get;
            set;
        }

        // The state of the animation
        public bool Active
        {
            get;
            set;
        }

        // Determines if the animation will keep playing or deactivate after one run
        public bool Looping
        {
            get;
            set;
        }

        public bool StopOnLast
        {
            get;
            set;
        }


        // The position of a frame
        public Vector2 Position
        {
            get;
            set;
        }

        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frameTime, Color color, float scale, bool looping, bool stopOnLast)
        {
            // Keep a local copy of the values passed in
            this.color = color;

            Position = position;
            FrameWidth = frameWidth;
            FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frameTime;
            this.scale = scale;
            Looping = looping;
            StopOnLast = stopOnLast;

            spriteStrip = texture;

            // Set the elapsed time and currentFrame to zero
            elapsedTime = 0;

            currentFrame = 0;

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width 
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2, (int)Position.Y - (int)(FrameHeight * scale) / 2, (int)(FrameWidth * scale), (int)(FrameHeight * scale));

            //destinationRect = new Rectangle(400, 200, (int)(FrameWidth * scale), (int)(FrameHeight * scale));
            // Set the Animation to active by default
            Active = true;
        }

        public void Update(GameTime gameTime)
        {
            // Do not update the game if we are not active
            if (!Active)
                return;

            // Update the elapsed time
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            // If the elapsed time is larger than the frame time we need to switch frames 
            if (elapsedTime > frameTime)
            {
                // Move to the next frame 
                currentFrame++;

                // If the currentFrame is equal to frameCount reset currentFrame to zero 
                if (currentFrame == frameCount)
                {
                    if (StopOnLast)
                    {
                        currentFrame--;
                    }
                    else
                    {
                        currentFrame = 0;

                        // If we are not looping deactivate the animation 
                        if (!Looping)
                            Active = false;
                    }
                }

                // Reset the elapsed time to zero 
                elapsedTime = 0;
            }

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width
            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);

            // Grab the correct frame in the image strip by multiplying the currentFrame index by the frame width 
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2, (int)Position.Y - (int)(FrameHeight * scale) / 2, (int)(FrameWidth * scale), (int)(FrameHeight * scale));

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            // Only draw the animation when we are active 
            if (Active) 
            { 
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color); 
            }
        }


    }
}
